package com.spit.hkpost.ui.newfrg

import android.os.Bundle
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.kingja.loadsir.core.LoadService
import com.spit.hkpost.adapter.RfidListAdapter
import com.spit.hkpost.api.UIHelper
import com.spit.hkpost.base.BaseFragment
import com.spit.hkpost.bean.DataBean
import com.spit.hkpost.databinding.BNotTitleRecyclerBinding
import com.spit.hkpost.ext.init
import com.spit.hkpost.ext.setNbOnItemClickListener
import com.spit.hkpost.mar.eventViewModel
import com.spit.hkpost.viewmodel.AssetModel
import com.spit.hkpost.viewmodel.RfidListModel
import com.spit.hkpost.weight.recyclerview.SpaceItemDecoration
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.ext.nav
import java.text.SimpleDateFormat

/**
 * @Author nike
 * @Date 2023/7/27 16:18
 * @Description 全部
 */
class RfidAllFrg: BaseFragment<RfidListModel, BNotTitleRecyclerBinding>(){

    val adapter: RfidListAdapter by lazy { RfidListAdapter(arrayListOf(), 0) }

    val rfidListModel: RfidListModel by activityViewModels()

    val assetModel: AssetModel by activityViewModels()

    lateinit var loadsir: LoadService<Any>

    var parentId: Long = 0

    var position: Int = 0

    var searchText: String? = null

    var prefix: String? = null

    var fmIsVisible = false

    override fun initView(savedInstanceState: Bundle?) {
        arguments?.let {
            parentId = it.getLong("parentId")
            prefix = it.getString("prefix")
            position = it.getInt("position")
        }
        mDatabind.swipeRefresh.isEnabled = false

        //初始化recyclerView
        mDatabind.recyclerView.init(LinearLayoutManager(context), adapter).let {
            it.addItemDecoration(SpaceItemDecoration(ConvertUtils.dp2px(0f), ConvertUtils.dp2px(10f), true))
        }
        adapter.run {
            setNbOnItemClickListener { adapter, view, position ->
                val bean = mFilterList[position]
                UIHelper.startSearchRfidFrg(nav(), bean.epc)
            }
        }

        mViewModel.onRequest(parentId, 0, prefix!!)

        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onResume(owner: LifecycleOwner) {
                super.onResume(owner)
                fmIsVisible = true
                rfidListModel.mutableTitle.value = "${adapter.mFilterList.size}"
            }

            override fun onPause(owner: LifecycleOwner) {
                super.onPause(owner)
                fmIsVisible = false
                assetModel.epcData.value = null
            }

            override fun onDestroy(owner: LifecycleOwner) {
                super.onDestroy(owner)
                assetModel.epcData.value = null

                var bean = DataBean()
                bean.position = position
                bean.updateDate = TimeUtils.getNowString(SimpleDateFormat("yyyy-MM-dd"))
                bean.number = adapter.data.filter { it.status == 1 }.size
                bean.allNumber = adapter.data.size
                eventViewModel.updateRfidFileData.value = bean
            }
        })
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.listBean.observe(viewLifecycleOwner, Observer {
            adapter.setList(it)
            adapter!!.appendList(it)
            rfidListModel.mutableTitle.value = "${adapter.mFilterList.size}"
        })
        //搜索
        rfidListModel.searchText.observe(viewLifecycleOwner, {
            searchText = it
            adapter!!.filter.filter(searchText)
            if (viewLifecycleOwner.lifecycle.currentState == Lifecycle.State.RESUMED) {
                GlobalScope.launch(Dispatchers.Main) {
                    delay(300)
                    if (fmIsVisible)rfidListModel.mutableTitle.value = "${adapter.mFilterList.size}"
                }
            }
        })

        rfidListModel.epcUploadData.observe(viewLifecycleOwner, {bean ->
            if (bean == null)return@observe
            if (adapter.data.any { it == bean }) {
                return@observe
            }
            adapter.addData(bean)
            if (!StringUtils.isEmpty(searchText)){
                adapter!!.filter.filter(searchText)
            }
            GlobalScope.launch(Dispatchers.Main) {
                delay(300)
                if (mDatabind.recyclerView != null) {
                    mDatabind.recyclerView?.scrollToPosition(adapter.mFilterList.size - 1)
                }
                if (fmIsVisible)rfidListModel.mutableTitle.value = "${adapter.mFilterList.size}"
            }
        })
    }

}